package com.msos.servlet.sendMsgServlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.msos.model.customer.Customer;
import com.msos.model.respBean.RespBean;
import com.msos.service.sendMsgService.sendMsgService;
import com.msos.utils.CommonsUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/sendMsgServlet")
public class sendMsgServlet extends HttpServlet {
    com.msos.service.sendMsgService.sendMsgService sendMsgService=new sendMsgService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String acPhoNumber=request.getParameter("phoNumber");
        String msg=request.getParameter("message");
        System.out.println("phoNumber====="+acPhoNumber);
        System.out.println("msg======"+msg);
        Customer custo=new Customer();
        custo.setUsername("张三");
        custo.setPhone("17369427280");
        HttpSession session=request.getSession();
        session.setAttribute("loginUser",custo);
        Customer customer=(Customer)session.getAttribute("loginUser");
        Integer r1=sendMsgService.sendMsg(acPhoNumber,msg,customer.getPhone());
        RespBean respBean=null;
        if(r1== CommonsUtils.SUCCESS){
            respBean=RespBean.ok("发送成功");
        }else if (r1==CommonsUtils.FAIL){
            respBean=RespBean.error("发送失败");
        }else if (r1==CommonsUtils.NOTHING){
            respBean=RespBean.error("号码x不存在");
        }else{
            respBean=RespBean.error("发送x异常");
        }
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper om=new ObjectMapper();
        PrintWriter out = response.getWriter();
       String json= om.writeValueAsString(respBean);
        out.write(json);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.getRequestDispatcher("WEB-INF/jsp/sendMsg/sendMsg.jsp").forward(request,response);
    }
}
